package com.wangyonghao.harmonyocrs.slice;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wangyonghao.harmonyocrs.MainAbility;
import com.wangyonghao.harmonyocrs.ResourceTable;
import com.wangyonghao.harmonyocrs.dao.RecogHistoryDao;
import com.wangyonghao.harmonyocrs.pojo.res.RecogResult;
import com.wangyonghao.harmonyocrs.provider.RecogProvider;
import com.wangyonghao.harmonyocrs.utils.HttpUtils;
import com.wangyonghao.harmonyocrs.utils.ToastUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.window.service.WindowManager;
import ohos.data.DatabaseHelper;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HistoryAbilitySlice extends AbilitySlice {
    Image back;
    Image search;

    RecogHistoryDao recogHistoryDao;
    private ListContainer listContainer;
    Gson gson = new Gson();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_history);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN |
                WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS |
                WindowManager.LayoutConfig.MARK_TRANSLUCENT_NAVIGATION);
        recogHistoryDao = new RecogHistoryDao(new DatabaseHelper(this));
        initComponent();
        event();
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_history_list);

//        ArrayList<RecogHistory> datasList = getData();
        //创建一-个Item的管理员对象(适配器对象)
        //并把要展示的所有数据和要加载的页面传递过月
//        ItemProvider ip = new ItemProvider(datasList, this,recogHistoryDao);
        //把适配器交给列表容器组件
        getData();
    }

    public void getData() {
        ArrayList<RecogResult> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("currentPage", 1);
        map.put("size", 100);

        HttpUtils httpUtils = new HttpUtils();
        String s = httpUtils.postToFormURL("/recog/basic/listAll", map);
        System.out.println(s);
        if (s.contains("total") && s.contains("data") && s.contains("content")) {
            String date = s.substring(s.indexOf("["), s.lastIndexOf("]") + 1);
            Type type = new TypeToken<List<RecogResult>>() {
            }.getType();
            //解析
            list = gson.fromJson(date, type);
        }
        listContainer.setItemProvider(new RecogProvider(list, this));
    }
//            String s = httpUtils.postToRequestBody("/userLogin", "{\n" +
//                    "\t\"password\": \"123\",\n" +
//                    "\t\"username\": \"ls\"\n" +
//                    "}");
//    public ArrayList<RecogHistory> getData() {
//        return (ArrayList<RecogHistory>) recogHistoryDao.findRecogHistoryByKey("");
//    }

    private void event() {
        back.setClickedListener(e -> {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("com.wangyonghao.harmonyocrs")
                    .withAbilityName(MainAbility.class.getName())
                    .withAction("mainAbilitySlice")
                    .build();
            intent.setOperation(operation);
            startAbility(intent);
//            Intent i = new Intent();
//            present(new MainAbilitySlice(), i);
//            terminateAbility();
        });
        search.setClickedListener(e -> {
            ToastUtils.showDialog(this, "点击了查询图片按钮");
        });
    }

    private void initComponent() {
        back = (Image) findComponentById(ResourceTable.Id_history_back);
        search = (Image) findComponentById(ResourceTable.Id_history_search);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
